package com.example.shw

import android.app.AlarmManager
import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import android.widget.LinearLayout
import android.widget.Toast

class ChangeActivity : AppCompatActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val linearLayout = LinearLayout(this)
        val start = Button(this)
        val stop = Button(this)
        start.isEnabled = true
        stop.isEnabled = false
        start.text = "开始"
        stop.text = "停止"
        linearLayout.addView(start)
        linearLayout.addView(stop)
        setContentView(linearLayout)
        val intent = Intent(this@ChangeActivity, ChangeService::class.java)
        val pi = PendingIntent.getService(this@ChangeActivity, 0, intent, 0)
        start.setOnClickListener {
            val aManager = getSystemService(Service.ALARM_SERVICE) as AlarmManager
            aManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                0, 5000, pi)
            start.isEnabled = false
            stop.isEnabled = true
            Toast.makeText(this@ChangeActivity, "壁纸定时更换启动成功了",
                Toast.LENGTH_SHORT).show()
        }
        stop.setOnClickListener {
            start.isEnabled = true
            stop.isEnabled = false
            val aManager = getSystemService(Service.ALARM_SERVICE) as AlarmManager
            aManager.cancel(pi)
        }
    }
}